using PaintIsland;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ObserverController : MonoBehaviour
{
/*    private readonly Vector3 CUBE_OFFSET = new Vector3(0.5f, 0.5f, 0.5f);
    public Transform head;

    public GameObject locator;

    public float cameraSen;
    public float moveSpeed;

    private new Rigidbody rigidbody;

    public Text gui;
    public GameObject info;

    public World world;

    private bool uiSelectMode;


    private bool isUsable = false;
    private bool isESC = false;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
        uiSelectMode = false;
        rigidbody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
            isESC = true;
        } 
        else if (Input.GetMouseButtonDown(0) && !isUsable)
        {
            if (isESC)
            {
                HideCursor();
            }
        }
        var x = Input.GetAxis("Horizontal");
        var y = Input.GetAxis("Vertical");

        // Move
        if (Input.GetAxis("Jump") > 0)
        {
            rigidbody.AddForce(Vector3.up * 15);
        }

        

        float savedY = rigidbody.velocity.y;

        rigidbody.velocity = (head.forward * y + head.right * x).normalized * moveSpeed;
        rigidbody.velocity = new Vector3(rigidbody.velocity.x, Mathf.Clamp(savedY, -20.0f, 10.0f), rigidbody.velocity.z);

        if (uiSelectMode) return;

        // View

        Camera.main.transform.localRotation *= Quaternion.Euler(-Input.GetAxis("Mouse Y") * cameraSen, 0, 0);
        var cr = Camera.main.transform.localRotation;

        head.localRotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * cameraSen, 0);
        Camera.main.transform.localRotation = new Quaternion(Mathf.Clamp(cr.x, -0.8f, 0.7f), cr.y, cr.z, cr.w);

        gui.text = $"Head Euler: {Camera.main.transform.localRotation}\nPosition: {rigidbody.position}\nVelocity: {rigidbody.velocity}\n";


        // Show target and set block
        Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f));
        
        if (Physics.Raycast(ray, out RaycastHit hit, 16, LayerMask.GetMask("Chunk")))
        {
            locator.SetActive(true);
            var p = hit.point - 0.5f * hit.normal;
            var blockPos = new Vector3(Mathf.Floor(p.x), Mathf.Floor(p.y), Mathf.Floor(p.z));
            locator.transform.position = blockPos + CUBE_OFFSET;

            gui.text += $"Target Pos: {blockPos}\n";

            var block = world.GetBlock((int)blockPos.x, (int)blockPos.y, (int)blockPos.z);
            gui.text += $"Targeting: {block}";
            if (Input.GetMouseButtonDown(1))
            {
                if (UsableBlock.IsBlockUsable(block))
                {
                    Cursor.lockState = CursorLockMode.None;
                    Cursor.visible = true;
                    info.SetActive(true);
                    uiSelectMode = true;
                    isUsable = true;
                }
                else
                {
                    var setPos = blockPos + hit.normal;
                    world.PlaceBlock(BlockID.CraftTable, (int)setPos.x, (int)setPos.y, (int)setPos.z);
                }
            }
            else if (Input.GetMouseButtonDown(0))
            {
                world.PlaceBlock(BlockID.Air, (int)blockPos.x, (int)blockPos.y, (int)blockPos.z);
            }
        }
        else
        {
            locator.SetActive(false);
        }
    }

    public void HideCursor()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        uiSelectMode = false;
    }

    public void LeaveUsable()
    {
        isUsable = false;
    }*/
}
